Sams Publishing : Programming in Objective-C by By

Sams Publishing : Programming in Objective-C by By

Author:By
Language: eng
Format: epub
ISBN: 0-672-32586-1
Publisher: Sams Publishing


Program 13.13

#import <stdio.h> void copyString (char *to, char *from) { for ( ; *from != '\0'; ++from, ++to ) *to = *from; *to = '\0'; } int main (int argc, char *argv[]) { void copyString (char *to, char *from); char string1[] = "A string to be copied."; char string2[50]; copyString (string2, string1); printf ("%s\n", string2); copyString (string2, "So is this."); printf ("%s\n", string2); return 0; }

Program 13.13 Output

A string to be copied. So is this.

The copyString function defines the two formal parameters, to and from, as character pointers and not as character arrays as was done in the previous version of copyString. This reflects how these two variables will be used by the function.

A for loop is then entered (with no initial conditions) to copy the string pointed to by from into the string pointed to by to. Each time through the loop, the from and to pointers are each incremented by one. This sets the from pointer pointing to the next character that is to be copied from the source string and sets the to pointer pointing to the location in the destination string where the next character is to be stored.

When the from pointer points to the null character, the for loop is exited. The function then places the null character at the end of the destination string.

In the main routine, the copyString function is called twice—the first time to copy the contents of string1 into string2 and the second time to copy the contents of the constant character string "So is this." into string2.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.